package com.zs.games.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class Knife4jConfiguration {
    private final Environment environment;

    public Knife4jConfiguration(Environment environment) {
        this.environment = environment;
    }

    @Value("${swagger.path}")
    private String customPath;

    @Bean(value = "defaultApi")
    public Docket defaultApi() {
        String activeProfile = environment.getProperty("spring.profiles.active");
        String groupName="接口文档";
        Docket docket = new Docket(DocumentationType.OAS_30)
                .apiInfo(new ApiInfoBuilder()
                        .title("接口文档")
                        .description("# 这里记录服务端所有的接口的入参，出参等等信息")
                        .termsOfServiceUrl("http://127.0.0.1:8080")
                        .contact(new Contact("XXX有限公司", "http://127.0.0.1:7172", ""))
                        .version("1.0")
                        .build())
                //分组名称
                .groupName(groupName)
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.zs.games.controller"))
                .paths(PathSelectors.any())
                .build()
                ;
        if (!"dev".equals(activeProfile)){
            docket.pathMapping(customPath);
        }

        return docket;
    }
}
